package com.wrx.server.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wrx.server.mapper.BarMapper;
import com.wrx.server.mapper.ReplyPostMapper;
import com.wrx.server.mapper.ThemePostMapper;
import com.wrx.server.pojo.Bar;
import com.wrx.server.pojo.ReplyPost;
import com.wrx.server.pojo.ReplyPostModel;
import com.wrx.server.pojo.RespBean;
import com.wrx.server.service.IBarService;
import com.wrx.server.service.IReplyPostService;
import com.wrx.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wrx
 * @since 2021-04-20
 */
@Service
public class ReplyPostServiceImpl extends ServiceImpl<ReplyPostMapper, ReplyPost> implements IReplyPostService {

    @Autowired
    ReplyPostMapper replyPostMapper;

    @Autowired
    ThemePostMapper themePostMapper;

    @Autowired
    IUserService userService;

    @Autowired
    BarMapper barMapper;
    @Override
    public RespBean putPageReplyPost(int theme_post_id, int pageNum, int pageSize,int orderByType) {
        PageHelper.startPage(pageNum,pageSize);
        List<ReplyPostModel> lists;

        lists = replyPostMapper.selAllReplyPostModel(theme_post_id);
        PageInfo<ReplyPostModel> pageInfo = new PageInfo<ReplyPostModel>(lists);
        return RespBean.success("success",pageInfo);
    }

    @Override
    public RespBean addReplyPost(ReplyPost replyPost, HttpServletRequest request) {


        Integer user_id = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (0==user_id&&null==user_id) {
            return RespBean.error("用户登录状态异常，请重新登录");
        }

        if (null==replyPost) {
            return RespBean.error("回帖失败");
        }

        if (StringUtils.isBlank(replyPost.getReply_post_info())){
            return RespBean.error("回帖内容为空，请重试");
        }


        replyPost.setUser_id(user_id);
        replyPost.setReply_post_floor(themePostMapper.selThemePostReplyNumber(replyPost.getTheme_post_id())+1);//设置楼层
        replyPostMapper.addReplyPost(replyPost);
        int state = replyPost.getReplyPostId();
        if (state>0){
            themePostMapper.addReplyThemePostNumber(replyPost.getTheme_post_id());//主题帖回复数+1
            return RespBean.success("回帖成功！");
        }


        return RespBean.success("回帖异常错误，请重试");
    }

    @Override
    public RespBean delReplyPost(ReplyPostModel replyPostModel, HttpServletRequest request) {
        Integer user_id = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (0==user_id&&null==user_id) {
            return RespBean.error("用户登录状态异常，请重新登录");
        }

        if (replyPostModel.getReply_post_id()==null ||replyPostModel.getReply_post_id()<=0 ) {
            return RespBean.error("帖子id异常");
        }
        ReplyPost replyPost = new ReplyPost(replyPostModel.getReply_post_id(),user_id,replyPostModel.getTheme_post_id());

        //del_status为1代表回复拥有者删除该回复，调用对应mapper
        if (replyPostModel.getDel_status()==1){
            if (replyPostMapper.delReplyPost(replyPost)==1){
                themePostMapper.delReplyThemePostNumber(replyPost.getTheme_post_id());
                return RespBean.success("删除回帖成功！");
            }else {
                return RespBean.error("删除回帖失败,回帖不存在");
            }
        }

        //del_status为2代表帖子拥有者删除该回复，调用对应mapper
        if (replyPostModel.getDel_status()==2){
            int themePostUserId = -1;
            themePostUserId =  themePostMapper.selThemePostUserId(replyPostModel.getTheme_post_id());
            if (user_id == themePostUserId){
                //当前用户id等同于帖子拥有人id时，即可删除
                if (replyPostMapper.delReplyPostByReplyId(replyPost.getReplyPostId())==1){
                    themePostMapper.delReplyThemePostNumber(replyPost.getTheme_post_id());
                    return RespBean.success("删除回帖成功！");
                }else {
                    return RespBean.error("删除回帖失败,回帖不存在");
                }
            }

        }

        if (replyPostModel.getDel_status()==3){
             int bar_owner_id =barMapper.selBarOwnerIdByThemePostId(replyPostModel.getTheme_post_id());
             if (user_id == bar_owner_id){
                 if (replyPostMapper.delReplyPostByReplyId(replyPost.getReplyPostId())==1){
                     themePostMapper.delReplyThemePostNumber(replyPost.getTheme_post_id());
                     return RespBean.success("删除回帖成功！");
                 }else {
                     return RespBean.error("删除回帖失败,回帖不存在");
                 }
             }
        }

        return RespBean.error("删除回帖异常，请重试");
    }
}
